package com.qipay.baas.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.qipay.baas.model.Order;
import com.qipay.baas.model.OrderExample.Criteria;
import com.qipay.baas.model.OrderExample.Criterion;
import com.qipay.baas.model.OrderExample;
import java.util.List;

public class OrderSqlProvider {

    public String countByExample(OrderExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("`order`");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(Order record) {
        BEGIN();
        INSERT_INTO("`order`");
        
        if (record.getCpId() != null) {
            VALUES("cp_id", "#{cpId,jdbcType=BIGINT}");
        }
        
        if (record.getBody() != null) {
            VALUES("body", "#{body,jdbcType=VARCHAR}");
        }
        
        if (record.getAmount() != null) {
            VALUES("amount", "#{amount,jdbcType=INTEGER}");
        }
        
        if (record.getFee() != null) {
            VALUES("fee", "#{fee,jdbcType=INTEGER}");
        }
        
        if (record.getFeeRecharge() != null) {
            VALUES("fee_recharge", "#{feeRecharge,jdbcType=DECIMAL}");
        }
        
        if (record.getActualAmount() != null) {
            VALUES("actual_amount", "#{actualAmount,jdbcType=INTEGER}");
        }
        
        if (record.getAdvanceAmount() != null) {
            VALUES("advance_amount", "#{advanceAmount,jdbcType=INTEGER}");
        }
        
        if (record.getDisableAmount() != null) {
            VALUES("disable_amount", "#{disableAmount,jdbcType=INTEGER}");
        }
        
        if (record.getAgentId() != null) {
            VALUES("agent_id", "#{agentId,jdbcType=BIGINT}");
        }
        
        if (record.getAgentFee() != null) {
            VALUES("agent_fee", "#{agentFee,jdbcType=INTEGER}");
        }
        
        if (record.getAgentFeeRecharge() != null) {
            VALUES("agent_fee_recharge", "#{agentFeeRecharge,jdbcType=DECIMAL}");
        }
        
        if (record.getState() != null) {
            VALUES("`state`", "#{state,jdbcType=SMALLINT}");
        }
        
        if (record.getTradeNo() != null) {
            VALUES("trade_no", "#{tradeNo,jdbcType=CHAR}");
        }
        
        if (record.getOutTradeNo() != null) {
            VALUES("out_trade_no", "#{outTradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getDescription() != null) {
            VALUES("description", "#{description,jdbcType=VARCHAR}");
        }
        
        if (record.getSpTradeNo() != null) {
            VALUES("sp_trade_no", "#{spTradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getSettleStatus() != null) {
            VALUES("settle_status", "#{settleStatus,jdbcType=TINYINT}");
        }
        
        if (record.getRemark() != null) {
            VALUES("remark", "#{remark,jdbcType=VARCHAR}");
        }
        
        if (record.getNotifyUrl() != null) {
            VALUES("notify_url", "#{notifyUrl,jdbcType=VARCHAR}");
        }
        
        if (record.getAttach() != null) {
            VALUES("attach", "#{attach,jdbcType=VARCHAR}");
        }
        
        if (record.getSpId() != null) {
            VALUES("sp_id", "#{spId,jdbcType=BIGINT}");
        }
        
        if (record.getChannelType() != null) {
            VALUES("channel_type", "#{channelType,jdbcType=TINYINT}");
        }
        
        if (record.getSpFeeRecharge() != null) {
            VALUES("sp_fee_recharge", "#{spFeeRecharge,jdbcType=DECIMAL}");
        }
        
        if (record.getSpFee() != null) {
            VALUES("sp_fee", "#{spFee,jdbcType=INTEGER}");
        }
        
        if (record.getSpMchId() != null) {
            VALUES("sp_mch_id", "#{spMchId,jdbcType=VARCHAR}");
        }
        
        if (record.getCreateTime() != null) {
            VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getFinishTime() != null) {
            VALUES("finish_time", "#{finishTime,jdbcType=TIMESTAMP}");
        }
        
        return SQL();
    }

    public String selectByExample(OrderExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("cp_id");
        SELECT("body");
        SELECT("amount");
        SELECT("fee");
        SELECT("fee_recharge");
        SELECT("actual_amount");
        SELECT("advance_amount");
        SELECT("disable_amount");
        SELECT("agent_id");
        SELECT("agent_fee");
        SELECT("agent_fee_recharge");
        SELECT("`state`");
        SELECT("trade_no");
        SELECT("out_trade_no");
        SELECT("description");
        SELECT("sp_trade_no");
        SELECT("settle_status");
        SELECT("remark");
        SELECT("notify_url");
        SELECT("attach");
        SELECT("sp_id");
        SELECT("channel_type");
        SELECT("sp_fee_recharge");
        SELECT("sp_fee");
        SELECT("sp_mch_id");
        SELECT("create_time");
        SELECT("actual_pay_amount");
        SELECT("finish_time");
        FROM("`order`");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        String tmp = "";
        if (example != null && example.getLimit() != null) {
            tmp = " limit " + example.getLimit().toString();
            if (example.getOffset() != null) {
                tmp = tmp + " offset " + example.getOffset().toString();
            }
        }
        return SQL() + tmp;
    }

    public String updateByPrimaryKeySelective(Order record) {
        BEGIN();
        UPDATE("`order`");
        
        if (record.getCpId() != null) {
            SET("cp_id = #{cpId,jdbcType=BIGINT}");
        }
        
        if (record.getBody() != null) {
            SET("body = #{body,jdbcType=VARCHAR}");
        }
        
        if (record.getAmount() != null) {
            SET("amount = #{amount,jdbcType=INTEGER}");
        }
        
        if (record.getFee() != null) {
            SET("fee = #{fee,jdbcType=INTEGER}");
        }
        
        if (record.getFeeRecharge() != null) {
            SET("fee_recharge = #{feeRecharge,jdbcType=DECIMAL}");
        }
        
        if (record.getActualAmount() != null) {
            SET("actual_amount = #{actualAmount,jdbcType=INTEGER}");
        }
        
        if (record.getAdvanceAmount() != null) {
            SET("advance_amount = #{advanceAmount,jdbcType=INTEGER}");
        }
        
        if (record.getDisableAmount() != null) {
            SET("disable_amount = #{disableAmount,jdbcType=INTEGER}");
        }
        
        if (record.getAgentId() != null) {
            SET("agent_id = #{agentId,jdbcType=BIGINT}");
        }
        
        if (record.getAgentFee() != null) {
            SET("agent_fee = #{agentFee,jdbcType=INTEGER}");
        }
        
        if (record.getAgentFeeRecharge() != null) {
            SET("agent_fee_recharge = #{agentFeeRecharge,jdbcType=DECIMAL}");
        }
        
        if (record.getState() != null) {
            SET("`state` = #{state,jdbcType=SMALLINT}");
        }
        
        if (record.getTradeNo() != null) {
            SET("trade_no = #{tradeNo,jdbcType=CHAR}");
        }
        
        if (record.getOutTradeNo() != null) {
            SET("out_trade_no = #{outTradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getDescription() != null) {
            SET("description = #{description,jdbcType=VARCHAR}");
        }
        
        if (record.getSpTradeNo() != null) {
            SET("sp_trade_no = #{spTradeNo,jdbcType=VARCHAR}");
        }
        
        if (record.getSettleStatus() != null) {
            SET("settle_status = #{settleStatus,jdbcType=TINYINT}");
        }
        
        if (record.getRemark() != null) {
            SET("remark = #{remark,jdbcType=VARCHAR}");
        }
        
        if (record.getNotifyUrl() != null) {
            SET("notify_url = #{notifyUrl,jdbcType=VARCHAR}");
        }
        
        if (record.getAttach() != null) {
            SET("attach = #{attach,jdbcType=VARCHAR}");
        }
        
        if (record.getSpId() != null) {
            SET("sp_id = #{spId,jdbcType=BIGINT}");
        }
        
        if (record.getChannelType() != null) {
            SET("channel_type = #{channelType,jdbcType=TINYINT}");
        }
        
        if (record.getSpFeeRecharge() != null) {
            SET("sp_fee_recharge = #{spFeeRecharge,jdbcType=DECIMAL}");
        }
        
        if (record.getSpFee() != null) {
            SET("sp_fee = #{spFee,jdbcType=INTEGER}");
        }
        
        if (record.getSpMchId() != null) {
            SET("sp_mch_id = #{spMchId,jdbcType=VARCHAR}");
        }
        
        if (record.getCreateTime() != null) {
            SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getFinishTime() != null) {
            SET("finish_time = #{finishTime,jdbcType=TIMESTAMP}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(OrderExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}